package com.distribution.system.scheduler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.distribution.system.domain.entity.CmccDaOrder;
import com.distribution.system.domain.entity.CmccDaUpChannelInfo;
import com.distribution.system.domain.entity.FuluUploadPeople;
import com.distribution.system.enums.OrderStatusEnum;
import com.distribution.system.mapper.CmccDaOrderMapper;
import com.distribution.system.service.CmccDaOrderService;
import com.distribution.system.service.CmccDaUpChannelInfoService;
import com.distribution.system.service.FuluUploadPeopleService;
import com.distribution.system.service.FuluUploadPlanService;
import com.distribution.system.utils.DateUtil;
import com.distribution.thread.ThreadPoolUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * 规则数据自动配置 调度器
 *
 * @author Liping Huo
 * @date 2023/5/12 14:08
 */
@Slf4j
@Component
public class OrderScheduling {
    @Resource
    CmccDaOrderMapper cmccDaOrderMapper;
    @Resource
    FuluUploadPeopleService fuluUploadPeopleService;
    @Resource
    FuluUploadPlanService fuluUploadPlanService;
    @Resource
    CmccDaOrderService cmccDaOrderService;
    @Resource
    CmccDaUpChannelInfoService cmccDaUpChannelInfoService;

    @XxlJob(value = "orderFailedMoreDay40")
    public void orderFailedMoreDay40(String param) {
        XxlJobHelper.log("orderFailedMoreDay40{}:",new Date());
        log.info("执行40天内未充值订单进行中转失败...");
        List<CmccDaOrder> orderList = cmccDaOrderService.lambdaQuery().isNotNull(CmccDaOrder::getOrderId)
                .in(CmccDaOrder::getOrderStatus, OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(), OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                .eq(CmccDaOrder::getProfitConditionStatus, 0)
                .gt(CmccDaOrder::getScMoney, 0)
                .le(CmccDaOrder::getCreateTime, DateUtil.addDate(new Date(), -40))
                .list();
        List<List<CmccDaOrder>> partition = ListUtils.partition(orderList, 100);
        for (List<CmccDaOrder> cmccDaOrderList : partition) {
            ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
                try {
                    Thread.sleep(100);
                    cmccDaOrderList.forEach(f -> {
                        f.setOrderStatus(OrderStatusEnum.FAIL.getOrderStatus());
                        f.setOrderStatusDesc(OrderStatusEnum.FAIL.getOrderStatusDesc());
                        f.setFailureReason("订单已超期");
                    });
                    cmccDaOrderService.updateBatchById(cmccDaOrderList);
                } catch (Exception e) {
                    log.error("执行40天内未充值订单进行中转失败发生异常{}", e.getMessage());
                }
            });
        }
    }



    /** ===================================================================7天内订单状态更新======================================================================== **/
    @XxlJob(value = "refreshLhtOrderStatusLimit7")
    public void refreshLhtOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshLhtOrderStatusLimit7{}:",new Date());
        log.info("执行【老后台100】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",100)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 老后台订单批量查单
            refreshOrderList(orderList, 100, 10);
        });
    }
    @XxlJob(value = "refreshYjOrderStatusLimit7")
    public void refreshYjOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshYjOrderStatusLimit7{}:",new Date());
        log.info("执行【樱角102】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",102)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 樱角订单批量查单
            refreshOrderList(orderList, 102, 10);
        });
    }
    @XxlJob(value = "refreshWhmOrderStatusLimit7")
    public void refreshWhmOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshWhmOrderStatusLimit7{}:",new Date());
        log.info("执行【王会明103】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",103)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 王会明订单批量查单
            refreshOrderList(orderList, 103, 10);
        });
    }
    // 【川禾】批量查询订单
    @XxlJob(value = "refreshChOrderStatusLimit7")
    public void refreshChOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshChOrderStatusLimit7{}:",new Date());
        log.info("执行【川禾107】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",107)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 川禾批量查单
            refreshOrderList(orderList, 107, 10);
        });
    }
    // 【三剑客】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshSjkOrderStatusLimit7")
    public void refreshSjkOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshSjkOrderStatusLimit7{}:",new Date());
        log.info("执行【三剑客104】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",104)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 三剑客批量查单
            refreshOrderList(orderList, 104, 60);
        });
    }
    // 【168】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshYlbOrderStatusLimit7")
    public void refreshYlbOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshYlbOrderStatusLimit7{}:",new Date());
        log.info("执行【168 108】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",108)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 168批量查单
            refreshOrderList(orderList, 108, 60);
        });
    }
    // 【秋葵】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshQkOrderStatusLimit7")
    public void refreshQkOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshQkOrderStatusLimit7{}:",new Date());
        log.info("执行【秋葵 106】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",106)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 秋葵批量查单
            refreshOrderList(orderList, 106, 60);
        });
    }
    // 奥亚7天内订单刷新
    @XxlJob(value = "refreshAyOrderStatusLimit7")
    public void refreshAyOrderStatusLimit7(String param) {
        XxlJobHelper.log("refreshAyOrderStatusLimit7{}:",new Date());
        log.info("执行【奥亚110】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",110)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(800);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新【奥亚110】订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }

    /**
     * 批量刷新指定渠道的订单
     * */
    private void refreshOrderList(List<CmccDaOrder> orderList, int channelId, Integer intervalSecond) {
        if (orderList.isEmpty()){
            log.info("批量刷新【{}】渠道订单时传入订单列表为空", channelId);
            return;
        }
        List<List<CmccDaOrder>> partition = ListUtils.partition(orderList, 100);
//        System.out.println("partition = " + partition.size());
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            int count = 0;
            @Override
            public void run() {
                log.info("批量刷新【{}】渠道订单：{}", channelId, partition.get(count).toString());
                cmccDaOrderService.refreshOrderList(partition.get(count), channelId);
                count++;
                if (count >= partition.size()) {
                    timer.cancel();
                    count = 0;
                }
            }
        }, 0, intervalSecond * 1000);
    }


    /** =================================================================7天外40天内订单状态更新====================================================================== **/
    @XxlJob(value = "refreshLhtOrderStatusLimit40")
    public void refreshLhtOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshLhtOrderStatusLimit40{}:",new Date());
        log.info("执行【老后台100】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",100)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 老后台订单批量查单
            refreshOrderList(orderList, 100, 10);
        });
    }
    @XxlJob(value = "refreshYjOrderStatusLimit40")
    public void refreshYjOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshYjOrderStatusLimit40{}:",new Date());
        log.info("执行【樱角102】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",102)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
//            System.out.println("orderList = " + orderList);
            // 樱角订单批量查单
            refreshOrderList(orderList, 102, 10);
        });
    }
    @XxlJob(value = "refreshWhmOrderStatusLimit40")
    public void refreshWhmOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshWhmOrderStatusLimit40{}:",new Date());
        log.info("执行【王会明103】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",103)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 王会明订单批量查单
            refreshOrderList(orderList, 103, 10);
        });
    }
    // 【川禾】批量查询订单
    @XxlJob(value = "refreshChOrderStatusLimit40")
    public void refreshChOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshChOrderStatusLimit40{}:",new Date());
        log.info("执行【川禾107】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",107)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 川禾批量查单
            refreshOrderList(orderList, 107, 10);
        });
    }
    // 【三剑客】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshSjkOrderStatusLimit40")
    public void refreshSjkOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshSjkOrderStatusLimit40{}:",new Date());
        log.info("执行【三剑客104】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",104)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 三剑客批量查单
            refreshOrderList(orderList, 104, 60);
        });
    }
    // 【168】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshYlbOrderStatusLimit40")
    public void refreshYlbOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshYlbOrderStatusLimit40{}:",new Date());
        log.info("执行【168 108】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",108)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 168批量查单
            refreshOrderList(orderList, 108, 60);
        });
    }
    // 【秋葵】订单查询接口有间隔60秒限制
    @XxlJob(value = "refreshQkOrderStatusLimit40")
    public void refreshQkOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshQkOrderStatusLimit40{}:",new Date());
        log.info("执行【秋葵 106】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",106)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> orderList = cmccDaOrderMapper.selectList(queryWrapper);
            // 秋葵批量查单
            refreshOrderList(orderList, 106, 60);
        });
    }
    // 奥亚7外40天内订单刷新
    @XxlJob(value = "refreshAyOrderStatusLimit40")
    public void refreshAyOrderStatusLimit40(String param) {
        XxlJobHelper.log("refreshAyOrderStatusLimit40{}:",new Date());
        log.info("执行【奥亚110】7天外40天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",110)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(800);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新【奥亚110】订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }



    /** ===================================================================其他任务======================================================================== **/
    @XxlJob(value = "autoUploadEvidence")
    public void autoUploadEvidence() {
        log.info("执行同步灵活用工数据...");
        List<FuluUploadPeople> list = fuluUploadPeopleService.lambdaQuery().eq(FuluUploadPeople::getDeleteFlag, 0)
                .eq(FuluUploadPeople::getSignStatus, 3)
                .list();
        if (!list.isEmpty()) {
            List<Long> collect = list.stream()
                    .map(FuluUploadPeople::getUserId)
                    .collect(Collectors.toList());
            List<CmccDaOrder> cmccDaOrderList = cmccDaOrderService.lambdaQuery().eq(CmccDaOrder::getOrderStatus, 3)
                    .gt(CmccDaOrder::getCreateTime, "2023-11-23")
                    .eq(CmccDaOrder::getFuluUploadStatus,0)
                    .in(CmccDaOrder::getUserId, collect)
                    .list();
            cmccDaOrderList.forEach(a->{
                try {
                    fuluUploadPlanService.uploadEvidence(a);
                } catch (Exception e) {
                    log.error("更新订单编号为{}的订单认证信息时发生异常{}", a.getOrderId(), e.getMessage());
                }
            });
        }
    }

    @XxlJob(value = "autoRefreshChannelCommission")
    public void autoRefreshChannelCommission() {
        XxlJobHelper.log("autoRefreshChannelCommission{}:",new Date());
        log.info("自动刷新各渠道方商品佣金...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            List<CmccDaUpChannelInfo> cmccDaUpChannelInfoList = cmccDaUpChannelInfoService.list();
            cmccDaUpChannelInfoList.forEach(x -> {
                try {
                    Thread.sleep(1000);
                    cmccDaOrderService.refreshChannelCommission(x);
                } catch (Exception e) {
                    log.error("刷新渠道方【{}】商品佣金时发生异常{}", x.getChannelName(), e.getMessage());
                }
            });
        });
    }































    @XxlJob(value = "orderFailedMoreDay30")
    public void orderFailedMoreDay30(String param) {
        XxlJobHelper.log("orderFailedMoreDay30{}:",new Date());
        log.info("执行30天内未充值订单进行中转失败...");
        List<CmccDaOrder> orderList = cmccDaOrderService.lambdaQuery().isNotNull(CmccDaOrder::getOrderId)
                .in(CmccDaOrder::getOrderStatus, OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(), OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                .eq(CmccDaOrder::getProfitConditionStatus, 0)
                .gt(CmccDaOrder::getScMoney, 0)
                .le(CmccDaOrder::getCreateTime, DateUtil.addDate(new Date(), -30))
                .list();
        List<List<CmccDaOrder>> partition = ListUtils.partition(orderList, 100);
        for (List<CmccDaOrder> cmccDaOrderList : partition) {
            ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(() -> {
                try {
                    Thread.sleep(100);
                    cmccDaOrderList.forEach(f -> {
                        f.setOrderStatus(OrderStatusEnum.FAIL.getOrderStatus());
                        f.setOrderStatusDesc(OrderStatusEnum.FAIL.getOrderStatusDesc());
                        f.setFailureReason("订单已超期");
                    });
                    cmccDaOrderService.updateBatchById(cmccDaOrderList);
                } catch (Exception e) {
                    log.error("执行15天内未充值订单进行中转失败发生异常{}", e.getMessage());
                }
            });
        }
    }



    /** ===================================================================7天内订单状态更新======================================================================== **/
    @XxlJob(value = "autoRuleMaintain")
    public void autoRuleMaintain(String param) {
        XxlJobHelper.log("进入autoRuleMaintain{}:",new Date());
        log.info("执行7天内订单状态同步（除103【王会明】, 104【三剑客】, 108【168】）...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .notIn("bind_channel_info_id", Arrays.asList(103, 104, 108))
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(100);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }
    @XxlJob(value = "autoRuleMaintainLimit")
    public void autoRuleMaintainLimit(String param) {
        XxlJobHelper.log("autoRuleMaintainLimit{}:",new Date());
        log.info("执行【王会明103】7天内订单状态同步...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",103)
                    .ge("create_time", DateUtil.addDate(new Date(),-7))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(800);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }

    /** =================================================================7天外40天内订单状态更新====================================================================== **/
    @XxlJob(value = "autoRuleMaintainLt30")
    public void autoRuleMaintainLt30(String param) {
        XxlJobHelper.log("autoRuleMaintainLt30{}:",new Date());
        log.info("执行7天外40天内订单状态同步（除103【王会明】, 104【三剑客】, 108【168】）...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .notIn("bind_channel_info_id", Arrays.asList(103, 104, 108))
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(500);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }
    @XxlJob(value = "autoRuleMaintainLimitLt30")
    public void autoRuleMaintainLimitLt30() {
        XxlJobHelper.log("autoRuleMaintainLimitLt30{}:",new Date());
        log.info("执行7天外40天内订单状态同步（103【王会明】）...");
        ThreadPoolUtil.THREAD_POOL_EXECUTOR_DEDUCT.execute(()->{
            QueryWrapper<CmccDaOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("profit_condition_status", 0)
                    .isNotNull("order_id")
                    .in("order_status", OrderStatusEnum.UN_ORDER_SUCCESS.getOrderStatus(),OrderStatusEnum.IN_PROGRESS.getOrderStatus())
                    .in("bind_channel_info_id",103)
                    .lt("create_time", DateUtil.addDate(new Date(),-7))
                    .ge("create_time", DateUtil.addDate(new Date(),-40))
                    .orderByDesc("create_time");
            List<CmccDaOrder> cmccDaOrders = cmccDaOrderMapper.selectList(queryWrapper);
            cmccDaOrders.forEach(x -> {
                try {
                    Thread.sleep(1000);
                    cmccDaOrderService.refreshOrder(x);
                } catch (Exception e) {
                    log.error("刷新订单编号为{}的订单发生异常{}", x.getOrderId(),e.getMessage());
                }
            });
        });
    }
}
